﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using QMSDAL;

namespace QuaManSys.Rights
{
    public partial class UserDetailPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "window.parent.changewin();", true);
            strinfo.InnerHtml = "";
            strinfo.Visible = false;
            if (!IsPostBack)
            {
                if (Request.QueryString["employeeId"] != null)
                {
                    GetUser(Request.QueryString["employeeId"].ToString());
                }

            }
        }

        protected void GetUser(string id)
        {
            CurUser person = Users.GetCurUser(id);
            using (var entity = new QuaManSysEntities())
            {
                //var queryUser = from q in entity.Tab_Person
                //                where q.employeeId == id
                //                select q;

                //Tab_Person person = queryUser.Single();
                lblId.Text = person.employeeId;
                lblName.Text = person.name;
                lblDept.Text = person.department;

                var queryUserRole = from q in entity.SysRoleSysPerson
                                    join r in entity.SysRole
                                    on q.SysRoleId equals r.Id
                                    where q.SysPersonId == person.employeeId
                                    select r.Name;


                List<string> rid = queryUserRole.ToList<string>();
                int n = rid.Count;
                string strRoles = "";
                if (n != 0)
                {
                    string[] str = new string[n];
                    int i = 0;
                    foreach (var item in rid)
                    {
                        str = rid[i].ToString().Split(',');
                        i++;
                    }
                    strRoles = StringArrayToString(str, '|');
                }
                else
                {
                    strRoles = "您暂没有为该用户分配任何权限";
                }
                lblRoles.Text = strRoles;
            }
        }

        /// <summary>
        /// 将字符串数组转成字符串
        /// </summary>
        /// <param name="strArray">字符串数组</param>        
        /// <param name="Separator">分隔符</param>
        /// <returns>字符串</returns>
        public static string StringArrayToString(string[] strArray, char Separator)
        {
            string str = string.Empty;
            for (int i = 0; i < strArray.Length; i++)
            {
                str = str + strArray[i] + Separator;
            }
            return str.Substring(0, str.Length - 1);
        }
    }
}